﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameHelper {

    /// <summary>
    /// 得到不满足的条件
    /// </summary>
    /// <param name="personage">人物</param>
    /// <param name="conditions">条件</param>
    /// <returns>List 不满足的条件（为空时表示满足)</returns>
    public static List<Constant.RequireCategory> GetDisSatisfactory(Personage personage, List<RequireCondition> conditions) {
        List<Constant.RequireCategory> requires = new List<Constant.RequireCategory>();
        foreach (RequireCondition requireCondition in conditions) {
            Constant.RequireCategory requireCategory = requireCondition.Prop;
            Constant.CompareCategory compareCategory = requireCondition.Compare;
            int requireNum = requireCondition.Val;
            if (requireCategory == Constant.RequireCategory.Hp && IsSatisfy(compareCategory, requireNum, personage.MaxHp)) { // 比较生命
                continue;
            } else if (requireCategory == Constant.RequireCategory.Mp && IsSatisfy(compareCategory, requireNum, personage.MaxMp)) { // 比较内力
                continue;
            } else if (requireCategory == Constant.RequireCategory.Attack && IsSatisfy(compareCategory, requireNum, personage.CurrentAttack)) { // 比较攻击
                continue;
            } else if (requireCategory == Constant.RequireCategory.Defense && IsSatisfy(compareCategory, requireNum, personage.CurrentDefense)) { // 比较防御
                continue;
            } else if (requireCategory == Constant.RequireCategory.Fist && IsSatisfy(compareCategory, requireNum, personage.CurrentFist)) { // 比较拳掌
                continue;
            } else if (requireCategory == Constant.RequireCategory.Leg && IsSatisfy(compareCategory, requireNum, personage.CurrentLeg)) { // 比较腿法
                continue;
            } else if (requireCategory == Constant.RequireCategory.Sword && IsSatisfy(compareCategory, requireNum, personage.CurrentSword)) { // 比较剑法
                continue;
            } else if (requireCategory == Constant.RequireCategory.Knife && IsSatisfy(compareCategory, requireNum, personage.CurrentKnife)) { // 比较刀法
                continue;
            } else if (requireCategory == Constant.RequireCategory.Stick && IsSatisfy(compareCategory, requireNum, personage.CurrentStick)) { // 比较棍棒
                continue;
            } else if (requireCategory == Constant.RequireCategory.Poison && IsSatisfy(compareCategory, requireNum, personage.CurrentPoison)) { // 比较毒术
                continue;
            } else if (requireCategory == Constant.RequireCategory.Leechcraft && IsSatisfy(compareCategory, requireNum, personage.CurrentLeechcraft)) { // 比较医术
                continue;
            } else if (requireCategory == Constant.RequireCategory.Power && IsSatisfy(compareCategory, requireNum, personage.CurrentPower)) { // 比较力量
                continue;
            } else if (requireCategory == Constant.RequireCategory.Speed && IsSatisfy(compareCategory, requireNum, personage.CurrentSpeed)) { // 比较身法
                continue;
            } else if (requireCategory == Constant.RequireCategory.Physique && IsSatisfy(compareCategory, requireNum, personage.CurrentPhysique)) { // 比较根骨
                continue;
            } else if (requireCategory == Constant.RequireCategory.Savvy && IsSatisfy(compareCategory, requireNum, personage.CurrentSavvy)) { // 比较悟性
                continue;
            } else if (requireCategory == Constant.RequireCategory.Tenacity && IsSatisfy(compareCategory, requireNum, personage.CurrentTenacity)) { // 比较坚韧
                continue;
            }
            requires.Add(requireCategory);
        }
        return requires;
    }

    /// <summary>
    /// 比较数值是否满足条件
    /// </summary>
    /// <param name="compareCategory">比较类型</param>
    /// <param name="requireNum">需要的数值</param>
    /// <param name="CurrentNum">当前数值</param>
    /// <returns></returns>
    private static bool IsSatisfy(Constant.CompareCategory compareCategory, int requireNum, int CurrentNum) { // 是否满足条件
        if (compareCategory == Constant.CompareCategory.Gte) { // 大于等于
            return CurrentNum >= requireNum;
        } else if (compareCategory == Constant.CompareCategory.Lte) { // 小于等于
            return CurrentNum <= requireNum;
        } else if (compareCategory == Constant.CompareCategory.Gt) { // 大于
            return CurrentNum > requireNum;
        } else if (compareCategory == Constant.CompareCategory.Lt) { // 小于
            return CurrentNum < requireNum;
        } else { // 等于
            return CurrentNum == requireNum;
        }
    }

    /// <summary>
    /// 整数乘以一个小数然后取整
    /// </summary>
    /// <param name="num1">整数</param>
    /// <param name="num2">小数</param>
    /// <returns>整数</returns>
    public static int Multi(int num1, double num2) {
        return (int) Math.Ceiling(num1 * num2);
    }

    /// <summary>
    /// 装备物品
    /// </summary>
    /// <param name="personage">人物</param>
    /// <param name="article">物品</param>
    public static void EquipArticle(Personage personage, Article article) {
        Constant.ArticleCategory articleCategory = article.ArticleTemplet.ArticleCategory;
        Article prevArticle = personage.EquipArticles[articleCategory];
        if (prevArticle != null) {
            UpdateArticle(personage, prevArticle, false);
        }
        personage.EquipArticles.Add(articleCategory, article);
        UpdateArticle(personage, article);
    }

    /// <summary>
    /// 更新装备
    /// </summary>
    /// <param name="personage">人物</param>
    /// <param name="article">装备物品</param>
    /// <param name="isPutOn">是否是穿上</param>
    private static void UpdateArticle(Personage personage, Article article, bool isPutOn = true) {
        int factor = isPutOn ? 1 : -1;
        personage.CurrentAttack += article.Attack * factor;
        personage.CurrentDefense += article.Defense * factor;
        personage.MaxHp += article.Hp * factor;
        personage.MaxMp += article.Mp * factor;
        personage.MaxVit += article.Vit * factor;
        personage.CurrentSpeed += article.Speed * factor;
    }
}
